package com.kzd.gtboon.main.home

import android.text.Editable
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.kzd.gtboon.R
import com.kzd.gtboon.app.BUNDLE_KEY_SEARCH
import com.kzd.gtboon.custom.setOnClickAntiShakeListener
import com.kzd.gtboon.databinding.ActivityHomeSearchBinding
import com.kzd.gtboon.jetpack.vm.SearchViewModel
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class HomeSearchActivity : OwnerViewBindingActivity<ActivityHomeSearchBinding>() {

    //搜索记录碎片
    private val mSearchRecordFragment: SearchRecordFragment by lazy {
        SearchRecordFragment.newInstance()
    }

    //搜索游戏碎片
    private val mSearchGameFragment: SearchGameFragment by lazy {
        SearchGameFragment.newInstance()
    }

    private val mViewModel: SearchViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityHomeSearchBinding
        get() = ActivityHomeSearchBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.llHomeSearchBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.ivHomeSearchBack.setOnClickListener { finish() }

        showRecordFragment()

        mViewBinding.tvHomeSearchSubmit.setOnClickAntiShakeListener {
            val content = mViewBinding.etHomeSearchContent.text.toString()
            if (content.isEmpty())
                return@setOnClickAntiShakeListener
            mViewModel.keywordLiveData.postValue(content)
        }
    }

    override fun initData() {
        mViewModel.searchRecord()
        mViewModel.searchHot()
        mViewModel.searchList()
    }

    override fun initViewMode() {
        mViewModel.keywordLiveData.observe(this as LifecycleOwner, { keyword ->

            mViewBinding.etHomeSearchContent.text =
                Editable.Factory.getInstance().newEditable(keyword)

            val searchList = mViewModel.mSearchRecordList.apply {
                remove(keyword)
                add(0, keyword)
            }
            SPStaticUtils.put(BUNDLE_KEY_SEARCH, GsonUtils.toJson(searchList))

            showGameFragment()
            mViewModel.searchInfo(keyword)
        })
    }

    private fun showRecordFragment() {
        supportFragmentManager.beginTransaction().apply {
            if (!mSearchRecordFragment.isAdded) {
                add(R.id.flHomeSearchBody, mSearchRecordFragment)
            } else if (mSearchRecordFragment.isHidden) {
                show(mSearchRecordFragment)
            }
            if (mSearchGameFragment.isAdded)
                hide(mSearchGameFragment)
        }.commit()
    }

    private fun showGameFragment() {
        supportFragmentManager.beginTransaction().apply {
            if (!mSearchGameFragment.isAdded) {
                add(R.id.flHomeSearchBody, mSearchGameFragment)
                addToBackStack(null)
            } else if (mSearchGameFragment.isHidden) {
                show(mSearchGameFragment)
            }
            if (mSearchRecordFragment.isAdded)
                hide(mSearchRecordFragment)
        }.commit()
    }
}